<?php

    class structure{
        private $dom = null;
        private $html = null;
        private $head = null;
        private $body = null;
        
        private $cssFiles = array(
            "css/layout.css"
        );
        
        private $class = array(
            "box" => "box",
            "dots" => "dots",
            "dot_top" => "dot-top",
            "dot_bottom" => "dot-bottom",
            "corner" => "corner",
            "line" => "line",
            "info" => "info",
            "title" => "title",
            "folder" => "folder",
            "file" => "file"
        );
    
        public function __construct() {
            // create dom
            $imp = new DOMImplementation();
            $html5 = $imp->createDocumentType('html');
            $this->dom = $imp->createDocument(null, null, $html5);
            
            // create default html structure
            $this->createHtml();
            $this->setHtmlHead();
        }
        
        public function addFolder($getTitle) {
            $box = $this->dom->createElement('div');
            $dots = $this->dom->createElement('div');
            $dot_top = $this->dom->createElement('div');
            $dot_bottom = $this->dom->createElement('div');
            $info = $this->dom->createElement('div');
            $item = $this->dom->createElement('div');
            $title = $this->dom->createElement('div', $getTitle);
            
            $box->setAttribute('class', $this->class["box"]);
            $dots->setAttribute('class', $this->class["dots"]);
            $dot_top->setAttribute('class', $this->class["dot_top"] . ' ' . $this->class["corner"]);
            $dot_bottom->setAttribute('class', $this->class["dot_bottom"] . ' ' . $this->class["line"]);
            $info->setAttribute('class', $this->class["info"]);
            $item->setAttribute('class', $this->class["folder"]);
            $title->setAttribute('class', $this->class["title"]);

            $this->body->appendChild($box);
            $box->appendChild($dots);
            $dots->appendChild($dot_top);
            $dots->appendChild($dot_bottom);
            $box->appendChild($info);
            $info->appendChild($item);
            $info->appendChild($title);
        }
        
        public function addFile($getTitle) {
            $box = $this->dom->createElement('div');
            $dots = $this->dom->createElement('div');
            $dot_top = $this->dom->createElement('div');
            $dot_bottom = $this->dom->createElement('div');
            $info = $this->dom->createElement('div');
            $item = $this->dom->createElement('div');
            $title = $this->dom->createElement('div', $getTitle);
            
            $box->setAttribute('class', $this->class["box"]);
            $dots->setAttribute('class', $this->class["dots"]);
            $dot_top->setAttribute('class', $this->class["dot_top"] . ' ' . $this->class["corner"]);
            $dot_bottom->setAttribute('class', $this->class["dot_bottom"] . ' ' . $this->class["line"]);
            $info->setAttribute('class', $this->class["info"]);
            $item->setAttribute('class', $this->class["file"]);
            $title->setAttribute('class', $this->class["title"]);

            $this->body->appendChild($box);
            $box->appendChild($dots);
            $dots->appendChild($dot_top);
            $dots->appendChild($dot_bottom);
            $box->appendChild($info);
            $info->appendChild($item);
            $info->appendChild($title);
        }
        
        public function save() {
            return $this->dom->saveHTML();
        }
        
        private function createHtml() {
            // html root <html>
            $this->html = $this->dom->createElement('html');
            $this->html->setAttribute('lang','en');
            $this->html = $this->dom->appendChild($this->html);

            // html <head>
            $this->head = $this->dom->createElement('head');
            $this->head = $this->html->appendChild($this->head);

            // html <body>
            $this->body = $this->dom->createElement('body');
            $this->body = $this->html->appendChild($this->body);
        }
        
        private function setHtmlHead() {
            $meta_charset = $this->dom->createElement('meta');
            $meta_charset->setAttribute('charset', 'utf-8');
            $this->head->appendChild($meta_charset);
            
            $this->addCss();

            $title = $this->dom->createElement('title', 'folder-structure');
            $this->head->appendChild($title);
        }
        
        private function addCss() {
            foreach ($this->cssFiles as $file) {
                $stylesheet = $this->dom->createElement('link');
                $stylesheet->setAttribute('rel', 'stylesheet');
                $stylesheet->setAttribute('type', 'text/css');
                $stylesheet->setAttribute('href', $file);
                $this->head->appendChild($stylesheet);
            }
        }
    }